package com.jia.controller;

import com.jia.pojo.Books;
import com.jia.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/books")
public class BooksController {

    @Autowired
    @Qualifier("BooksServiceImpl")
    private BooksService booksService;

    @RequestMapping("/allBook")
    public String list(Model model) {
        List<Books> books = booksService.queryAllBooks();
        model.addAttribute("list",books);
        return "allBook";
    }

    @RequestMapping("/toAddPage")
    public String toAddPage(Model model) {
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String addBook(Books books,Model model) {
        booksService.addBook(books);
        return "redirect:/books/allBook";
    }

    @RequestMapping("/toUpdatePage")
    public String toUpdatePage(int id,Model model) {
        Books books = booksService.queryBookById(id);
        model.addAttribute("qbook",books);
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Books books,Model model) {
        booksService.updateBook(books);
        return "redirect:/books/allBook";
    }

    @RequestMapping("/deleteBook/{id}")
    public String deleteBook(@PathVariable("id") int id){
        booksService.deleteBook(id);
        return "redirect:/books/allBook";
    }

}
